﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MyLibrary.API.Core;

namespace MyLibrary.API.IO
{
    public class CSVWriter
    {
        public string File { get; private set; }
        public bool Exists { 
            get
            {
                if (!string.IsNullOrEmpty(File))
                {
                    if (System.IO.File.Exists(File))
                        return true;
                }
                return false;
            }
        }

        private int m_Index;
        private Dictionary<int, Row> m_Rows;
    
        private CSVWriter()
        { }

        private CSVWriter(string location)
        {
            m_Index = 0;
            File = location;
            m_Rows = new Dictionary<int, Row>();
        }

        public Row this[int index]
        {
            get
            {
                return m_Rows[index]; 
            }
        }

        public int AddRow()
        {
            m_Index++;
            m_Rows.Add(m_Index, new Row());
            return m_Index;
        }

        public bool Flush()
        {
            if (m_Index > 0 && m_Rows.Count > 0)
            {
                using (FileStream FStream = new FileStream(File, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter WStream = new StreamWriter(FStream))
                    {
                        foreach (Row row in m_Rows.Values)
                        {
                            WStream.WriteLine(string.Join(",", row.Values.ToArray()));
                        }
                        WStream.Flush();
                    }
                }
                Clear();
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Clear()
        {
            m_Index = 0;
            m_Rows.Clear();
        }

        public static CSVWriter Create(string location)
        {
            return new CSVWriter(location);
        }

        public class Row
        {
            private List<string> m_Values;
            public List<string> Values
            {
                get
                {
                    if (m_Values == null)
                        m_Values = new List<string>();
                    return m_Values;
                }
            }
            public void AddEntry(string value)
            {
                Values.Add(CleanWritable(value));
            }

            private static string CleanWritable(string value)
            {
                if (string.IsNullOrEmpty(value)) return "";

                string stencil = (value.Contains(",")) ? "\"{0}\"" : "{0}";
                value = value.Replace("\"", "'");
                return string.Format(stencil, value);
            }
        }
    }
}
